<template>
	<view class="content">
		<button class="size" @tap="startPlay">开始播放音频</button>
		<button class="size" @tap="pausePlay">暂停播放音频</button>
		<button class="size" @tap="resumePlay">恢复播放音频</button>
		<button class="size" @tap="seekTo">跳到指定位置音频</button>
		<button class="size" @tap="stopPlay">结束播放音频</button>
		<button class="size" @tap="getDuration">获取音频的总长度</button>
		<button class="size" @tap="getPosition">获取音频的当前播放位置 {{ position}}</button>
		<button class="size" @tap="paused"> 当前是否暂停或停止状态 {{ pausedStatus ?"暂停/停止":"播放"}}</button>
		<button class="size" @tap="backRate">设置倍速播放</button>
		
		<view class="size">------------------</view>
		
		<button class="size" @tap="startRecord">开始录制音频</button>
		<button class="size" @tap="stopRecord">结束录制音频</button>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				p:null,
				r:null,
				currentRecord:"/static/13978.wav",
				position:0,
				pausedStatus:-1
			}
		},
		onLoad() {
			this.onPlusReady()
		},
		methods: {
			createPlayer(){
				
			},
			startPlay(){
				if(plus.audio == undefined){
					console.log("设备没有准备好")
				};
				// 创建音频播放器，不同系统，支持的音频格式有区别
				console.log(this.currentRecord,"this.currentRecord11")
				this.p = plus.audio.createPlayer( this.currentRecord );
				// 听筒线路
				this.p.setRoute( plus.audio.ROUTE_EARPIECE );
				this.p.play( function () {
					console.log( "Audio play success!" ); 
					this.backRate()
				}, function ( e ) {
					Object.keys(e).forEach(key=>{
						console.log( "Audio play error: " + key + e[key]);
					})
				});
			},
			stopPlay(){
				// 停止播放
				this.p.stop();
			},
			pausePlay(){
				this.p.pause();
			},
			resumePlay(){
				this.p.resume();
			},
			seekTo(){
				this.p.seekTo(6);
			},
			getDuration(){
				const duration =  this.p.getDuration();
				console.log(duration,"duration==")
			},
			getPosition(){
				this.position = this.p.getPosition();
			},
			paused(){
				this.pausedStatus = this.p.isPaused();
			},
			backRate(){
				this.p.playbackRate(2)
			},
			startRecord(){
				if(this.r == null){
					console.log("设备还没有准备")
					return 
				}
				// 首先授权允许访问麦克风，其次点击开始录音，录好就可以结束录音，filename保存录音文件的路径
				// record 录音操作， stop 录音结束
				this.r.record( {filename:"_doc/testAudio/"}, function (e) {
						console.log( "Audio record success!" ,e);
						this.currentRecord = e;
				}, function ( e ) {
						console.log( "Audio record failed: " + e.message );
				} );
			},
      stopRecord(){
				this.r.stop(); 
			},
			onPlusReady(){
				this.r = plus.audio.getRecorder();
			},
			
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	.size{
		margin:30rpx 0;
	}
</style>
